package com.dgut.service.serviceImpl;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.mapper.ServiceProviderMapper;
import com.dgut.common.SelectRole;
import com.dgut.mapper.AdministratorMapper;
import com.dgut.mapper.BuyerMapper;
import com.dgut.mapper.UserMapper;
import com.dgut.pojo.Administrator;
import com.dgut.pojo.Buyer;
import com.dgut.pojo.User;
import com.dgut.pojo.receive.AdministratorAndBuyerReceive;
import com.dgut.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings({"all"})

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private BuyerMapper buyerMapper;
    @Resource
    private ServiceProviderMapper serviceProviderMapper;
    @Resource
    private AdministratorMapper administratorMapper;


    /**
     * start-zwb
     */
    //返回用户信息
    public Object returnUserInformation(Integer id)
    {
        User user = userMapper.selectById(id);
        if(SelectRole.BUYER.equals(user.getRole()))
            return buyerMapper.selectById(id);
        else  if(SelectRole.SERVICE_PROVIDER.equals(user.getRole()))
            return serviceProviderMapper.selectById(id);
        else  if(SelectRole.ADMIN.equals(user.getRole()))
            return administratorMapper.selectById(id);
        else
            return user;
    }
    /**
     * end-zwb
     */


    /**
     * start-gxd
     */
    /**
     * end-gxd
     */



    /**
     * start-lzh
     */
    //用户修改自己的密码
    @Override
    @SaCheckLogin
    public Object UpdateUserPassword(Integer id,String password1,String password2){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if(userMapper.UpdateUserPasword(id,password1,password2)!=0){
            resultMap.put("message","修改密码成功！");
        }
        else {
            resultMap.put("message","修改密码失败！");
        }
        return resultMap;
    }
    /**
     * end-lzh
     */

    /**
     * start-hwf
     */

    @Override
    public boolean check(User user) {
        boolean result = false;
        Map map = new Hashtable();
        map.put("username",user.getUsername());
        List<User> list = userMapper.selectByMap(map);
        User u = null;
        if(list.size() == 0){
            return false;
        }else{
            u = list.get(0);
        }
        if(u != null && u.getPassword().equals(user.getPassword())){
            user.setRole(u.getRole());
            user.setId(u.getId());
            result = true;
        }
        return result;
    }



    @Override
    public String getRole(Integer userId) {
        return userMapper.selectById(userId).getRole();
    }

    @Override
    public User getUser(int userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public boolean updateUser(User user) {
        if(userMapper.updateById(user) >= 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteUser(int id) {
        if(userMapper.deleteById(id) >= 1){
            return true;
        }else{
            return false;
        }
    }



    @Override
    public boolean resetPassword(AdministratorAndBuyerReceive administratorAndBuyerReceive) {
        boolean result = false;
        Map map = new Hashtable();
        map.put("username",administratorAndBuyerReceive.getUsername());
        List<User> list = userMapper.selectByMap(map);
        if(list.size() != 0){
            User user = list.get(0);
            switch (user.getRole()){
                case SelectRole.BUYER:
                    Buyer buyer = buyerMapper.selectById(user.getId());
                    if(administratorAndBuyerReceive.getIdCard().equals(buyer.getIdCard())&&administratorAndBuyerReceive.getName().equals(buyer.getName())){
                        user.setPassword(administratorAndBuyerReceive.getPassword());
                        userMapper.updateById(user);
                        result = true;
                    }
                    break;
                case SelectRole.ADMIN:
                    Administrator administrator = administratorMapper.selectById(user.getId());
                    if(administratorAndBuyerReceive.getIdCard().equals(administrator.getIdCard())&&administratorAndBuyerReceive.getName().equals(administrator.getName())){
                        user.setPassword(administratorAndBuyerReceive.getPassword());
                        userMapper.updateById(user);
                        result = true;
                    }
                    break;
            }
        }
        return result;
    }
    /**
     * end-hwf
     */
}
